package com.demo.onvif.controller;

import com.alibaba.fastjson.JSONObject;
import com.demo.onvif.result.AjaxResult;
import com.demo.onvif.service.IOnvifService;
import de.onvif.soap.OnvifDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.xml.soap.SOAPException;
import java.io.IOException;

@RestController
@RequestMapping("/onvif")
public class OnvifController {
    @Autowired
    private IOnvifService onvifService;

    @GetMapping("/search")
    public AjaxResult discoverOnvif(String ip) throws IOException {
        return AjaxResult.success(onvifService.discoverOnvif(ip));
    }

    @PostMapping("/channel")
    public AjaxResult channelOnvif(@RequestBody JSONObject jsonObject) throws IOException, SOAPException {
        OnvifDevice onvifDevice = new OnvifDevice(jsonObject.getString("address"),
                jsonObject.getString("userName"),
                jsonObject.getString("password"));
        return AjaxResult.success(onvifService.getOnvifChannel
                (onvifDevice));
    }
}
